package com.delivery.www.servlet;

import com.alibaba.fastjson.JSON;
import com.delivery.www.po.Ban;
import com.delivery.www.po.Store;
import com.delivery.www.po.User;
import com.delivery.www.constant.Constants;
import com.delivery.www.service.BanService;
import com.delivery.www.service.ServiceImpl.BanServiceImpl;
import com.delivery.www.service.ServiceImpl.StoreServiceImpl;
import com.delivery.www.service.ServiceImpl.UserServiceImpl;
import com.delivery.www.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 12970
 */
public class UserServlet extends BaseServlet {
    UserService userService = new UserServiceImpl();
    BanService banService = new BanServiceImpl();

    /**
     * 获取封禁用户
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void banUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<Ban> bans = banService.getBan();
        List<User> users = userService.getBanUser(bans);
        writer.print(JSON.toJSONString(users));
    }

    /**
     * 获取用户
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(user));
    }

    /**
     * 充值
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void recharge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        PrintWriter writer = resp.getWriter();
        Float money = Float.valueOf(req.getParameter("money"));
        User recharge = userService.recharge(user.getUserId(), money);
        req.getSession().setAttribute(Constants.USER_SESSION, recharge);
        if (recharge != null) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 获取所有用户
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<User> users = userService.getUser();
        writer.print(JSON.toJSONString(users));
    }

    /**
     * 注销
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        req.getSession().removeAttribute(Constants.USER_SESSION);
        if (req.getSession().getAttribute(Constants.USER_SESSION) == null) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 登录
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        User user;
        if (account.contains(Constants.CHAR)) {
            user = userService.emailLogin(account, password);
        } else {
            user = userService.userNameLogin(account, password);
        }
        if (user != null) {
            req.getSession().setAttribute(Constants.USER_SESSION, user);
            writer.print(JSON.toJSONString(user));
        } else {
            writer.print("error");
        }
    }

    /**
     * 注册判断
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void registerJudge(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        String account = req.getParameter("account");
        String email = req.getParameter("email");
        List<User> users = userService.getUser();
        for (User user : users) {
            if (user.getUserName().equals(account) || user.getEmail().equals(email)) {
                writer.print("error");
                break;
            }
        }
    }

    /**
     * 注册
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        String account = req.getParameter("account");
        String email = req.getParameter("email");
        String registerPassword = req.getParameter("registerPassword");
        String s = req.getParameter("identity");
        int identity;
        if (s != null) {
            identity = Integer.parseInt(s);
            boolean b = userService.register(account, registerPassword, email, identity);
            if (b) {
                writer.print("ok");
            } else {
                writer.print("error");
            }
        } else {
            writer.print("error");
        }
    }

    /**
     * 用户的店铺
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void store(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        StoreServiceImpl storeService = new StoreServiceImpl();
        Store store = storeService.getOwnStore(user.getUserId());
        writer.print(JSON.toJSONString(store));
    }
}
